package com.kehongborui.empmanagesys.util;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief SqlSessionFactory 工具类，该类是线程安全的单例模式
 * @email 122162005@qq.com
 * @date 2018-07-23
 */
public class SqlSessionFactoryUtil {
    private static SqlSessionFactory sqlSessionFactory;

    private SqlSessionFactoryUtil() {}

    public static SqlSessionFactory getSqlSessionFactory() {
        if (sqlSessionFactory == null) {
            synchronized (SqlSessionFactoryUtil.class) {
                if (sqlSessionFactory == null) {
                    // 1.获取mybatis 配置
                    String resource = "mybatis-config.xml";
                    // 2. 把配置转换成流
                    try {
                        // 3. 获取 sqlSessionFactory
                        sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return sqlSessionFactory;
    }

    public static SqlSessionFactory getSqlSessionFactory02() {
        if (sqlSessionFactory == null) {
            synchronized (SqlSessionFactoryUtil.class) {
                if (sqlSessionFactory == null) {
                    // 1. 够Configuration 对象
                    Configuration configuration = new Configuration();

                    // 配置data source
                    PooledDataSource dataSource = new PooledDataSource();
                    Properties properties = new Properties();
                    try {
                        properties.load(Resources.getResourceAsStream("com/kehongborui/empmanagesys/util/db.properties"));
                        dataSource.setUrl(properties.getProperty("jdbc.url"));
                        dataSource.setUsername(properties.getProperty("jdbc.username"));
                        dataSource.setPassword(properties.getProperty("jdbc.password"));
                        dataSource.setDriver(properties.getProperty("jdbc.driver"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    configuration.getTypeAliasRegistry().registerAliases("com.kehongborui.empmanagesys.entity");

                    configuration.setEnvironment(new Environment(
                            "development",
                            new JdbcTransactionFactory(),
                            dataSource));
                    // 注册 mapper
                    configuration.getMapperRegistry().addMappers("com.kehongborui.empmanagesys.mapper");
                    // 2. 获取 sqlSessionFactory
                    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
                }
            }
        }

        return sqlSessionFactory;
    }

    /**
     * 获取 session
     * @return
     */
    public static SqlSession getSession() {
        return getSqlSessionFactory().openSession();
    }
}
